<?php

namespace Cps\Controller;

use Think\Controller;

class LinkController extends Controller {

    public function __construct() {
        parent::__construct();
        session("[start]");
        if (!isset($_SESSION['cpsinfo']) || $_SESSION['cpsinfo']['id'] < 1) {
            $this->redirect('Login/index');
        }
    }

    public function createlink() {
        $game_id = intval($_POST['game_id']);
        $area_num = intval($_POST['area_num']);

        //获得enname
        $gameModel = M('game_list');
        $game = $gameModel->field('enname')->where("id='%d'", $game_id)->find();

        session("['start']");
        $cpsinfo = $_SESSION['cpsinfo'];
        $tg_game_info = M('tg_game_info');
        $data['refname'] = $cpsinfo['username'];
        $data['game_id'] = $game_id;
        $data['area_num'] = $area_num;
        if ($tg_game_info->data($data)->add()) {
            $referid = $tg_game_info->getLastInsID();
            //需要把referid更新到cps_member对应的refer
            $cpsmemberModel = new \Cps\Model\CpsmemberModel();
            $cpsmember = $cpsmemberModel->getByCpsUser($cpsinfo['username']);
            if ($cpsmember['refer'] == 0) {//说明一个都没有，直接update
                $data['refer'] = $referid;
                $cpsmemberModel->where("id='%d'", array($cpsinfo['id']))->save($data);
            } else {//说明，已经有ID，需要做处理
                $temp = explode(',', $cpsmember['refer']);
                $temp[] = $referid;
                $refer = implode(',', $temp);
                $data2['refer'] = $refer;                
                $cpsmemberModel->where("id='%d'", array($cpsinfo['id']))->save($data2);
                $_SESSION['cpsinfo']['refer']=$data2['refer'];
            }
            $url = "http://tg.678fu.com/" . $game['enname'] . "/index.php?game_id=" . $game_id . "&area_num=" . $area_num . "&refer=" . $referid;
            $data1['refurl'] = $url;
            if ($tg_game_info->where("id='%d'", array($referid))->save($data1)) {                
                $this->ajaxReturn(array("ret" => 0, "msg" => $url), 'json');
            } else {
                $this->ajaxReturn(array("ret" => 1, "msg" => "系统忙，请稍后在试！"), 'json');
            }
        }
    }

    public function shortLink() {
        $id = intval($_POST['id']);
        $longUrl = I('post.url');
        $apiKey = '569452181'; //要修改这里的key再测试哦
        $apiUrl = 'https://api.weibo.com/2/short_url/shorten.json?source=' . $apiKey . '&url_long=' . urlencode($longUrl);
        $response = file_get_contents($apiUrl);
        $json = json_decode($response);
        $shoturls = $json->urls;
        if ($shoturls) {
            $tg_game_info = M('tg_game_info');
            $data['shorturl'] = $shoturls[0]->url_short;
            $tg_game_info->where("id='%d'", $id)->save($data);
            $this->ajaxReturn(array('ret' => 0, 'msg' => $shoturls[0]->url_short), 'json');
        } else {
            $this->ajaxReturn(array('ret' => 1, 'msg' => '网络忙，请稍后在试！'), 'json');
        }

        echo json_encode($shoturls[0]->url_short);
        exit;
    }

}
